package com.zhan.ielts.dao.impl.word;

import com.zhan.frame.core.dao.impl.BaseDaoImpl;
import com.zhan.frame.core.exception.DaoException;
import com.zhan.frame.core.utils.BeanUtils;
import com.zhan.ielts.dao.word.WordIeltsDao;
import com.zhan.ielts.domain.WordIelts;
import com.zhan.ielts.domain.vo.word.WordIeltsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class WordIeltsDaoImpl extends BaseDaoImpl<WordIelts> implements WordIeltsDao {
	@Override
	public void saveOrUpdate(WordIelts entity) {
		try {
			if (entity != null) {
				if (StringUtils.isBlank(entity.getId()))
					entity.setId(generateId());
				sqlSessionTemplate.insert(getSqlName("saveOrUpdate"), entity);
			}
		} catch (Exception e) {
			throw new DaoException(String.format("添加对象出错！语句：%s", getSqlName("saveOrUpdate")), e);
		}

	}

	@Override
	public PageImpl<WordIeltsVo> selectWordList(WordIelts wordIelts, Pageable pageable) {
		List<WordIeltsVo> contentList = sqlSessionTemplate.selectList(getSqlName("selectWord"),
				getParams(wordIelts, pageable));
		return new PageImpl<WordIeltsVo>(contentList, pageable, this.selectWordCount(wordIelts));
	}

	@Override
	public Long selectWordCount(WordIelts query) {
		Map<String, Object> params = BeanUtils.toMap(query);
		return sqlSessionTemplate.selectOne(getSqlName("selectWordCount"), params);
	}

}
